Borland Online And The Cobb Group Present:


October, 1994 - Vol. 1 No. 10

Letters - Menu enabling and disabling in OWL 2.0

In Where do TCommandEnabler objects come from? from the July 1994 issue of Borland C++ Developer's Journal, you stated that the ObjectWindows Library (OWL) code will disable new menu commands by default unless you provide a command enabler function for those commands. This doesn't seem to be correct.

In the ENABLER.CPP program you used as an example, if you remove the command enabler functions from the response table, you'll notice that nothing disables the menu commands. What's going on here?

Don Nelson

via fax

Don, what we told you was partially correct. In reality, OWL will disable menu commands if it can't find a command enabler function (this is what we said), or if it can't find a function to handle that command in one of the current response tables (we left out that part).

You'll find the code that implements this search in the \FRAMEWIN.CPP file from the \BC4\OWL\SOURCE directory. On line 177, you'll see

EvCommandEnable(TMenuItemEnabler(hPopupMenu, 
    id, HWindow, pos));

This line calls the EvCommandEnable() member function with a new TMenuItemEnabler object. This TMenuItemEnabler object contains a handle to the menu, the ID number of the command, a handle to the window, and the position of the command in the menu.

Inside the EvCommandEnable() member function (beginning on line 238 of the same file), you'll see that the function first checks to see if there is an enabler for this ID in the current command chain (by calling the EvCommandEnable() function for the front window). If there isn't an enabler in the command chain, the function then searches for any event-handling function that handles a command with this ID by calling

focusWindow->Find(eventInfo)

on line 282.

This is why eliminating the command enablers causes OWL to enable the menu items all the time. Command enabler functions no longer exist for those command IDs, but there are event-handling functions for them: TEnablerApp::EvMenu1() and TEnablerApp::EvMenu2().

To see how this works in the example program from the article you mentioned, change the response table definition that appears in the ENABLER.CPP listing to match the following:

DEFINE_RESPONSE_TABLE(TEnablerApp, TApplication)
  EV_COMMAND(101, EvMenu1),
  EV_COMMAND_ENABLE(101, EvMenu1Enable),
END_RESPONSE_TABLE;

Now, the response table includes only entries for the EvMenu1 command function and the EvMenu1Enable command enabler function.

When you rebuild the application, you'll see that OWL disables Item 2 in the Command menu all the time, but Item 1 continues to display normally. However, if you select Item 1, its command enabler function will disable it. Since Item 2 isn't available, you won't be able to select it to toggle the cmdFlag variable back to the previous state, and neither menu command will be available, as shown in Figure A.


Figure A - If you remove the event handler and the command enabler for Item 2, OWL will always disable Item 2.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.